package com.cqeec.gams.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqeec.gams.entity.Role;
import com.cqeec.gams.entity.RolePermission;
import com.cqeec.gams.mapper.RoleMapper;
import com.cqeec.gams.mapper.RolePermissionMapper;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public long countAll() {
		return roleMapper.countAll();
	}

	@Override
	public long count(String name) {
		return roleMapper.count(name);
	}

	@Override
	public List<Role> findAll() {
		return roleMapper.findAll();
	}

	@Override
	public void insert(Role role) {
		roleMapper.insert(role);
	}

	@Override
	public void update(Role role) {

	}

	@Override
	public void delete(String id) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.RoleService#page(int, int)
	 */
	@Override
	public List<Role> page(String name, int pageNumber, int pageSize) {
		return roleMapper.selectPage(name, (pageNumber - 1) * pageSize, pageSize);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.RoleService#save(com.cqeec.gams.entity.Role)
	 */
	@Override
	public void save(Role role) {
		role.setId(UUID.randomUUID().toString());
		roleMapper.save(role);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.RoleService#save(com.cqeec.gams.entity.Role,
	 * java.lang.String[])
	 */
	@Override
	public void save(Role role, String[] function) {
		if (role.getId() == null || "".equals(role.getId())) {
			role.setId(UUID.randomUUID().toString());
			roleMapper.save(role);
		} else {
			roleMapper.update(role);
			rolePermissionMapper.deleteByRole(role.getId());
		}
		if (function != null && function.length > 0) {
			rolePermissionMapper.insert(role.getId(), role.getName(), function);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.RoleService#get(java.lang.String)
	 */
	@Override
	public Role get(String id) {
		return roleMapper.findById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cqeec.gams.service.RoleService#getRolePermission(java.lang.String)
	 */
	@Override
	public List<RolePermission> getRolePermission(String id) {
		return rolePermissionMapper.findByRole(id);
	}

}